﻿
using Dapper;
using HBase;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/

    public class StockUtil {

        static public CS getCoreStock<CS>(IDbConnection Connection ,IDbTransaction transaction ,long id) where CS : AbsCoreStock {
            String sql = @"select * from core_stock where id=@id ";
            List<CS> list = Connection.Query<CS>(sql ,new {
                id = id
            } ,transaction).ToList();
            if( list.Count == 1 ) {
                return list[0];
            } else if( list.Count > 1 ) {
                throw new Exception(id + "货位记录过多");
            } else {
                return null;
            }
        }
        static public void taskDeleteUpdateStock(IDbConnection Connection ,IDbTransaction transaction ,long id ,int src) {
            String sql1 = @"select status from core_stock where id=@id ";
            List<int> statusList = Connection.Query<int>(sql1 ,new { id } ,transaction).ToList();
            if( statusList.Count == 0 ) {
                throw new Exception(id + "无有效记录");
            }
            int status = statusList[0];
            int locId = src;
            if( status == ( int ) StockStatus.盘库中 ) {
                status = ( int ) StockStatus.盘库中;
            } else if( !LocConfig.isLoc(src) ) {
                status = ( int ) StockStatus.删除;
            } else if( LocConfig.isLoc(src) ) {
                status = ( int ) StockStatus.库存;
            } else {
                throw new Exception("未知任务类型" + id);
            }
            string sql = @"update Core_stock set Status=@status,loc_Id=@locId, change_Time=sgj_get_sys_now where id=@id ";
            GkTransaction.exec(Connection ,transaction ,sql ,new { id ,status ,locId });
        }

        static public bool UpdateStock(IDbConnection Connection ,IDbTransaction transaction ,long stockId ,StockStatus status) {
            string sql = "update  Core_Stock set status=@status where id=@Id";
            int count = Connection.Execute(sql ,new { id = stockId ,status = ( int ) status } ,transaction);


            return true;
        }
        static public bool UpdateStockWithLoc(IDbConnection Connection ,IDbTransaction transaction ,long stockId ,long locId ,StockStatus status) {
            string sql = string.Format("update  Core_Stock set status='{0}',loc_Id='{1}'  where id='{2}'" ,( int ) status ,locId ,stockId);
            int count = Connection.Execute(sql ,null ,transaction);

            string sql1 = string.Format("update  core_stock_detail set  r_loc_Id='{0}'  where stock_Id='{1}'" ,locId ,stockId);
            Connection.Execute(sql1 ,null ,transaction);


            return true;
        }

        static public CS getStock<CS>(IDbConnection conn ,IDbTransaction transaction ,long id) where CS : AbsCoreStock {
            return conn.GetById<CS>(id ,transaction);
        }

    }
}
